
#.SUFFIXES: .c .cpp

ifeq ($(DEBUG), 1)
	DEFINES = -DDEBUG -D_DEBUG -DLINUX -ggdb -O0
	ODIR = ./build/Linux/Debug

	LIB_MGLIB = ./MGLib/build/Linux/Debug/libMGLib.a
	LIBS_MGLIB = -L./MGLib/build/Linux/Debug -lMGLib

	LIB_TINYXML = ./lib/tinyxml/build/Linux/Debug/libtinyxml.a
	LIBS_TINYXML = -L./lib/tinyxml/build/Linux/Debug -ltinyxml

	BUILD = DEBUG=1
else
	DEFINES = -DLINUX -DNDEBUG -O2
	ODIR = ./build/Linux/Release

	LIB_MGLIB = ./MGLib/build/Linux/Release/libMGLib.a
	LIBS_MGLIB = -L./MGLib/build/Linux/Release -lMGLib

	LIB_TINYXML = ./lib/tinyxml/build/Linux/Release/libtinyxml.a
	LIBS_TINYXML = -L./lib/tinyxml/build/Linux/Release -ltinyxml

	BUILD = 
endif

SDIR = ./src/
IDIR = ./src/
LIB_MATH = ./lib/math/glmath/lib/Linux/libglmath.a
LIB_LUA = ./lib/lua/build/Linux/liblua.a

INCLUDE_MGLIB = -I./MGLib/include/
INCLUDE_GLMATH = -I./lib/math/glmath/include/
INCLUDE_LUA = -I./lib/lua/src/
INCLUDE_TINYXML = -I./lib/tinyxml/src/
INCLUDE = -I$(IDIR) $(INCLUDE_MGLIB) $(INCLUDE_LUA) $(INCLUDE_TINYXML)

CC = g++

#LIBS_GLMATH = -L./lib/math/glmath/lib/Linux -lglmath
LIBS_LUA = -L./lib/lua/src -llua
LIBS = $(LIBS_MGLIB) $(LIBS_LUA) $(LIBS_TINYXML) -lGL -lGLU -lglut -lCg -lCgGL -lGLEW

CFLAGS = -Wall $(DEFINES) $(INCLUDE)

_SRCS = SM.cpp CSM.cpp RSM.cpp Deferred.cpp Tests.cpp
_OBJS = Application.o main.o $(_SRCS:.cpp=.o)
_DEPS = $(_SRCS:.cpp=.h)

SRCS = $(patsubst %,$(SDIR)/%,$(_SRCS))
OBJS = $(patsubst %,$(ODIR)/%,$(_OBJS))
DEPS = $(patsubst %,$(IDIR)/%,$(_DEPS))

PRJ = $(ODIR)/proiect

build:	all

all:	build-deps	build-local

build-deps:	$(LIB_MGLIB)	$(LIB_LUA)	$(LIB_TINYXML)

build-local:	build-path	$(PRJ)

$(LIB_MGLIB):
	cd MGLib && make $(BUILD)

$(LIB_LUA):
	cd ./lib/lua/ && make linux

$(LIB_GLMATH):
	cd lib/math/glmath && make
	
$(LIB_TINYXML):
	cd lib/tinyxml && make $(BUILD)

$(PRJ):	$(OBJS)
	$(CC) $^ $(CFLAGS) $(LIBS) -o $@

$(ODIR)/%.o:	$(SDIR)/%.cpp	$(DEPS)
	$(CC) -c $< -o $@ $(CFLAGS)

build-path:	$(ODIR)

$(ODIR):
	mkdir -p $(ODIR)

.PHONY:	clean
	
clean:	clean-deps	clean-local

clean-local:	clean-edits
	rm -fr $(ODIR)
	
clean-deps:
	cd ./MGLib/ && make clean
	cd ./lib/lua/ && make clean
	cd ./lib/math/glmath/ && make clean
	cd ./lib/tinyxml/ && make clean

clean-edits:
	rm -rf `find . -name *~`
	
rebuild:	clean	all

run:	build
	cd ./workDir && ../$(PRJ)

debug:	build
	cd ./workDir && gdb ../$(PRJ)

